﻿using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;

namespace BootstrapBlazor.Components;

/// <summary>
/// 下拉按钮
/// </summary>
public sealed partial class DropdownButton
{
    /// <summary>
    /// 获得/设置 PopoverConfirm 服务实例
    /// </summary>
    [Inject]
    [NotNull]
    private PopoverService? PopoverService { get; set; }

    [Inject]
    [NotNull]
    private IStringLocalizer<PopConfirmButton>? Localizer { get; set; }

    /// <summary>
    /// 获得/设置 下拉按钮按钮项
    /// </summary>
    [Parameter]
    public IEnumerable<DropdownButtonItem> Items { get; set; } = Enumerable.Empty<DropdownButtonItem>();

    /// <summary>
    /// 获得/设置 颜色 默认 Color.None 无设置
    /// </summary>
    [Parameter]
    public Color Color { get; set; }

    /// <summary>
    /// 获得/设置 是否开启分裂式 默认 false
    /// </summary>
    [Parameter]
    public bool ShowSplit { get; set; }

    /// <summary>
    /// 获得/设置 获取菜单对齐方式 默认 none 未设置
    /// </summary>
    [Parameter]
    public Alignment MenuAlignment { get; set; }

    /// <summary>
    /// 获得/设置 下拉选项方向 默认 Dropdown 向下
    /// </summary>
    [Parameter]
    public Direction Direction { get; set; }

    /// <summary>
    /// 获得/设置 组件尺寸 默认 none 未设置
    /// </summary>
    [Parameter]
    public Size Size { get; set; } = Size.ExtraSmall;

    /// <summary>
    /// 获得/设置 下拉框渲染类型 默认 DropdownMenu 下拉菜单
    /// </summary>
    [Parameter]
    public DropdownType DropdownType { get; set; } = DropdownType.ButtonGroup;

    /// <summary>
    /// 获得/设置 显示文字
    /// </summary>
    [Parameter]
    public string Text { get; set; } = string.Empty;

    private string? _confirmButtonText;
    private string? _closeButtonText;
    private string? _content;

    /// <summary>
    /// 获得 按钮弹出方向集合
    /// </summary>
    /// <returns></returns>
    private string? DirectionClassName => CssBuilder.Default()
        .AddClass($"btn-group", DropdownType == DropdownType.ButtonGroup)
        .AddClass($"{Direction.ToDescriptionString()}", DropdownType == DropdownType.DropdownMenu)
        .AddClassFromAttributes(AdditionalAttributes)
        .Build();

    /// <summary>
    /// 获得 按钮样式集合
    /// </summary>
    /// <returns></returns>
    private string? ButtonClassName => CssBuilder.Default("btn")
        .AddClass("dropdown-toggle", !ShowSplit)
        .AddClass($"btn-primary", Color == Color.None)
        .AddClass($"btn-{Color.ToDescriptionString()}", Color != Color.None)
        .AddClass($"btn-{Size.ToDescriptionString()}", Size != Size.None)
        .Build();

    /// <summary>
    /// 获得 按钮样式集合
    /// </summary>
    /// <returns></returns>
    private string? ClassName => CssBuilder.Default("btn dropdown-toggle")
      .AddClass("dropdown-toggle-split")
      .AddClass($"btn-{Color.ToDescriptionString()}", Color != Color.None)
      .AddClass($"btn-{Size.ToDescriptionString()}", Size != Size.None)
      .Build();

    /// <summary>
    /// 获得 是否分裂式按钮
    /// </summary>
    private string? DropdownToggle => !ShowSplit ? "dropdown" : null;

    /// <summary>
    /// 菜单对齐方式样式
    /// </summary>
    private string? MenuAlignmentClass => CssBuilder.Default("dropdown-menu shadow")
        .AddClass($"dropdown-menu-{MenuAlignment.ToDescriptionString()}", MenuAlignment == Alignment.Right)
        .Build();

    /// <summary>
    /// 获得/设置 设置当前项是否 Active 方法
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    private static string? ActiveItem(DropdownButtonItem item) => CssBuilder.Default("dropdown-item")
        .AddClass("active", () => item.IsActive)
        .Build();

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        _confirmButtonText ??= Localizer[nameof(PopConfirmButtonBase.ConfirmButtonText)];
        _closeButtonText ??= Localizer[nameof(PopConfirmButtonBase.CloseButtonText)];
        _content ??= Localizer[nameof(PopConfirmButtonBase.Content)];
    }

    /// <summary>
    /// 下拉框选项点击时调用此方法
    /// </summary>
    private async Task OnItemClick(DropdownButtonItem item)
    {
        //调整选择状态
        foreach (var button in Items)
        {
            button.IsActive = button.Id == item.Id;
        }

        //判断是否需要确认弹窗
        if (item.IsConfirmButton)
        {
            await PopoverService.Show(BuildConfirmOption(item));
        }
        else if (item.OnConfirm != null)
        {
            await item.OnConfirm();
        }
    }

    /// <summary>
    /// 组装弹窗配置项
    /// </summary>
    /// <param name="option"></param>
    /// <returns></returns>
    private PopoverConfirmOption BuildConfirmOption(DropdownButtonItem item)
    {
        return new PopoverConfirmOption()
        {
            ButtonId = Id,
            Title = item.Title,
            Content = item.Content ?? _content,
            CloseButtonText = item.CloseButtonText ?? _closeButtonText,
            CloseButtonColor = item.CloseButtonColor,
            ConfirmButtonText = item.ConfirmButtonText ?? _confirmButtonText,
            ConfirmButtonColor = item.ConfirmButtonColor,
            Icon = item.ConfirmIcon,
            OnConfirm = item.OnConfirm,
            OnClose = item.OnClose,
            Callback = async () =>
            {
                // 调用 JS 进行弹窗 等待 弹窗点击确认回调
                await JSRuntime.InvokeVoidAsync("$.bb_confirm", Id);
            }
        };
    }
}
